package com.atguigu.day02;

/**
 * @ClassName ProdConsumerDemo
 * @Description 此类描述:
 * @Author dangchen
 * @DateTime 2022-09-07 18:52
 * @Version 1.0
 */

import javafx.beans.binding.NumberBinding;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 有一个初始值为0的变量,现有两个线程对该变量操作,
 * 实现一个线程对变量加1
 * 实现一个线程对变量减1
 * 交替进行10轮
 *
 * 1.高内聚低耦合前提下  线程 操作  资源类
 * 2.判断  干活  通知
 * 3.防止线程的虚假唤醒  wait方法用while,用if要担心虚假唤醒,如果是两个线程就不用担心了,非此即彼,不会有第三个来抢
 * if之所以要担心虚假唤醒,是因为if只判断一次,之后就运行if判断之后的代码,但是while是循环判断,只要不满足循环条件,退出循环,才会执行while之后的代码
 */
class AirCondition{
    /*private int number = 0;

    public synchronized void increment() throws InterruptedException {
        //1.判断
        while (number != 0){
            this.wait();
        }
        //2.干活
        System.out.println(Thread.currentThread().getName() + "\t" + (++number));
        //3.通知
        this.notifyAll();
    }

    public synchronized void decrement() throws InterruptedException {
        //1.判断
        while (number == 0){
            this.wait();
        }
        //2.干活
        System.out.println(Thread.currentThread().getName() + "\t" + (--number));
        //3.通知
        this.notifyAll();
    }*/

    //----------JUC新版本
    private int number = 0;

    private Lock lock = new ReentrantLock();
    private Condition condition = lock.newCondition();

    public void increment() throws InterruptedException {
        lock.lock();
        try {
            //1.判断
            while (number != 0){
                condition.await();//this.wait();
            }
            //2.干活
            System.out.println(Thread.currentThread().getName() + "\t" + (++number));
            //3.通知
            condition.signalAll();//this.notifyAll();
        }finally {
            lock.unlock();
        }
    }

    public void decrement() throws InterruptedException {
        lock.lock();
        try {
            while (number == 0){
                //1.判断
                condition.await();//this.wait();
            }
            //2.干活
            System.out.println(Thread.currentThread().getName() + "\t" + (--number));
            //3.通知
            condition.signalAll();//this.notifyAll();
        }finally {
            lock.unlock();
        }
    }

}
public class ProdConsumerDemo {
    public static void main(String[] args) {
        AirCondition airCondition = new AirCondition();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    airCondition.increment();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"A").start();

        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    airCondition.decrement();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"B").start();

        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    airCondition.increment();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"C").start();

        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    airCondition.decrement();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"D").start();
    }
}
